/**
 * May 5, 2014 4:59:31 PM
 */
package elitech.vietnam.copyweatherfashion.fragments.styler;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTabHost;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import elitech.vietnam.copyweatherfashion.LoginActivity;
import elitech.vietnam.copyweatherfashion.MainActivity;
import elitech.vietnam.copyweatherfashion.R;
import elitech.vietnam.copyweatherfashion.utilities.MyCallable;
import elitech.vietnam.copyweatherfashion.utilities.Utilities;

/**
 * @author Cong
 *
 */
public class StylerFragment extends Fragment {
	
	public static final String TAG_ALL = "TAGALL";
	public static final String TAG_FRI = "TAGFRIENDS";

	MainActivity mActivity;
	
	FragmentTabHost mTabHost;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mActivity = (MainActivity) getActivity();
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.styler, container, false);
		
		mTabHost = (FragmentTabHost) view.findViewById(R.id.styler_tabhost);
		
		mTabHost.setup(mActivity, getChildFragmentManager(), R.id.styler_tabcontent);
		mTabHost.addTab(mTabHost.newTabSpec(TAG_ALL).setIndicator(createTabView(R.drawable.allstyle_s, R.drawable.allstyle_d, getString(R.string.all_styler))),
				StylerContentFragment.class, StylerContentFragment.newArs(TAG_ALL));
		if (mActivity.mUser != null)
			mTabHost.addTab(mTabHost.newTabSpec(TAG_FRI).setIndicator(createTabView(R.drawable.frstyle_s, R.drawable.frstyle_d, getString(R.string.followed_styler))),
					StylerContentFragment.class, StylerContentFragment.newArs(TAG_FRI));
		
		return view;
	}
	
	@Override
	public void onStart() {
		super.onStart();
		/*
		 * Initialize header view
		 */
		mActivity.mTxtTitle.setText(Html.fromHtml(getString(R.string.stylertitle)));
		mActivity.mBtnBack.setVisibility(View.GONE);
		mActivity.mBtnCamera.setVisibility((mActivity.mUser!=null) ? View.VISIBLE : View.GONE);
		mActivity.mBtnEdit.setVisibility(View.GONE);
		mActivity.mBtnNotify.setVisibility((mActivity.mUser!=null) ? View.VISIBLE : View.GONE);
	}
	
	@Override
	public void onResume() {
		super.onResume();
		/**
		 * Implement onActivityResult Base on MainActivity.mRequestCode and
		 * MainActivity.mResultIntent
		 */
		if (mActivity.mRequestCode == MainActivity.REQUEST_LOGIN && mActivity.mResultIntent != null) {
			mActivity.getLoggedMemberInfo(mActivity.mResultIntent.getIntExtra(LoginActivity.EXTRA_USERID, -1),
					new MyCallable() {
						@Override
						public void call() {
							// Let the notify button appear
							mActivity.mBtnNotify.setVisibility((mActivity.mUser != null) ? View.VISIBLE : View.GONE);
							// Reload all tabs
							mTabHost.clearAllTabs();
							mTabHost.addTab(mTabHost.newTabSpec(TAG_ALL).setIndicator(createTabView(R.drawable.allstyle_s, R.drawable.allstyle_d, getString(R.string.all_styler))),
									StylerContentFragment.class, StylerContentFragment.newArs(TAG_ALL));
							if (mActivity.mUser != null)
								mTabHost.addTab(mTabHost.newTabSpec(TAG_FRI).setIndicator(createTabView(R.drawable.frstyle_s, R.drawable.frstyle_d, getString(R.string.followed_styler))),
										StylerContentFragment.class, StylerContentFragment.newArs(TAG_FRI));
							// Delete the result
							mActivity.mRequestCode = -1;
							mActivity.mResultIntent = null;
						}
					});
		}
	}
	
	private View createTabView(int selected, int normal, String title) {
		View view 			= LayoutInflater.from(mActivity).inflate(R.layout.styler_tab, null);
		TextView mTitle 	= (TextView) view.findViewById(R.id.stylertab_txtText);
		ImageView mImage 	= (ImageView) view.findViewById(R.id.stylertab_imgImage);
		Drawable drawable 	= Utilities.getTabDrawable(mActivity, selected, normal);
		mImage.				setImageDrawable(drawable);
		mTitle.				setText(title);
		return view;
	}
	
	@Override
	public void onDestroyView() {
		super.onDestroyView();
	}
}
